package com.scce.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Author: luoxuyang
 * @Date: 2023/3/25 22:40
 * @Description: com.scce.config
 */
public class DynamicDataSourceHolder {
    private static Logger logger = LoggerFactory.getLogger(DynamicDataSourceHolder.class);

    private static ThreadLocal<String> contextHolder = new ThreadLocal<String>();

    public static final String DB_MASTER = "master";

    public static final String DB_SLAVE = "slave";

    /**
     * 获取数据库类型
     *
     * @return
     */
    public static Object getDbType() {
        String db = contextHolder.get();
        logger.info("获取数据库类型", db);
        if (db == null) {
            return DB_MASTER;
        }
        return db;
    }

    /**
     * 设置连接类型
     *
     * @param target
     */
    public static void setDbType(String target) {
        logger.debug("所使用的数据源为：" + target);
        contextHolder.set(target);
    }

    /**
     * 清除连接类型
     */
    public static void clearDbType() {
        contextHolder.remove();
    }
}
